﻿using PaddleOCRSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PaddleOCRSharp_Demo
{
    public class TestInvoice
    {
        public void Test(string fileName)
        {
            //使用默认中英文V3模型
            OCRModelConfig config = null;
            //使用默认参数
            OCRParameter oCRParameter = new OCRParameter();

            OCRResult ocrResult = new OCRResult();

            //建议程序全局初始化一次即可，不必每次识别都初始化，容易报错。
            PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);
            {
                ocrResult = engine.DetectText(fileName);
            }

            if (ocrResult != null)
            {
                foreach (var item in ocrResult.TextBlocks)
                {
                    Console.WriteLine(item.Text);
                }
                Console.WriteLine("**************************");
                Console.WriteLine(ocrResult.Text);
                Console.WriteLine("******************************");

                foreach (var item in ocrResult.TextBlocks)
                {
                    if (item.Text.Contains("发票号码")|| item.Text.ToUpper().Contains("NO"))
                    {
                        var regex = new Regex("\\d*$");
                        var mc = regex.Match(item.Text);
                        if (mc.Success)
                        {
                            Console.WriteLine($"发票号码：{mc.Value.Substring(0, 8)}");
                        }
                    }
                    if (item.Text.Contains("开票日期"))
                    {
                        var regex = new Regex("\\d{4}年\\d{2}月\\d{2}日");
                        var mc = regex.Match(item.Text);
                        if (mc.Success)
                        {
                            DateTime time = Convert.ToDateTime(mc.Value.Replace("年", "/").Replace("月", "/").Replace("日", ""));
                            Console.WriteLine($"开票日期：{time}");
                        }
                        else
                        {
                            string tempStr = item.Text.Replace("开票日期：", "");
                            DateTime.TryParse(tempStr, out DateTime time1);
                            Console.WriteLine($"开票日期：{time1}");
                        }
                    }
                    if (item.Text.Contains("%"))
                    {
                        var regex = new Regex("^\\d*.\\d*");
                        var mc = regex.Match(item.Text);
                        if (mc.Success)
                        {
                            decimal.TryParse(mc.Value, out decimal tempValue);
                            Console.WriteLine($"税率：{tempValue}");
                        }
                    }
                    if (item.Text.Contains("￥"))
                    {
                        var regex = new Regex("\\d.\\d*");
                        var mc = regex.Match(item.Text);
                        if (mc.Success)
                        {
                            Console.WriteLine($"金额：{decimal.Parse(mc.Value)}");
                        }
                    }

                }

            }
        }
    }
}
